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-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *  net/dccp/proto.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *  An implementation of the DCCP protocol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *  Arnaldo Carvalho de Melo <acme@conectiva.com.br>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/dccp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/in.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <net/checksum.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <net/inet_sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <net/inet_common.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <net/xfrm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <asm/ioctls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include "ccid.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include "dccp.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include "feat.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #define CREATE_TRACE_POINTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #include "trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) DEFINE_SNMP_STAT(struct dccp_mib, dccp_statistics) __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) EXPORT_SYMBOL_GPL(dccp_statistics);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) struct percpu_counter dccp_orphan_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) EXPORT_SYMBOL_GPL(dccp_orphan_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) struct inet_hashinfo dccp_hashinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) EXPORT_SYMBOL_GPL(dccp_hashinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) /* the maximum queue length for tx in packets. 0 is no limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) int sysctl_dccp_tx_qlen __read_mostly = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) #ifdef CONFIG_IP_DCCP_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) static const char *dccp_state_name(const int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 	static const char *const dccp_state_names[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	[DCCP_OPEN]		= "OPEN",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	[DCCP_REQUESTING]	= "REQUESTING",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	[DCCP_PARTOPEN]		= "PARTOPEN",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	[DCCP_LISTEN]		= "LISTEN",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	[DCCP_RESPOND]		= "RESPOND",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	[DCCP_CLOSING]		= "CLOSING",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	[DCCP_ACTIVE_CLOSEREQ]	= "CLOSEREQ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	[DCCP_PASSIVE_CLOSE]	= "PASSIVE_CLOSE",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	[DCCP_PASSIVE_CLOSEREQ]	= "PASSIVE_CLOSEREQ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	[DCCP_TIME_WAIT]	= "TIME_WAIT",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	[DCCP_CLOSED]		= "CLOSED",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	if (state >= DCCP_MAX_STATES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 		return "INVALID STATE!";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 		return dccp_state_names[state];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) void dccp_set_state(struct sock *sk, const int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	const int oldstate = sk->sk_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	dccp_pr_debug("%s(%p)  %s  -->  %s\n", dccp_role(sk), sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 		      dccp_state_name(oldstate), dccp_state_name(state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	WARN_ON(state == oldstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	switch (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	case DCCP_OPEN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 		if (oldstate != DCCP_OPEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 			DCCP_INC_STATS(DCCP_MIB_CURRESTAB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 		/* Client retransmits all Confirm options until entering OPEN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 		if (oldstate == DCCP_PARTOPEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 			dccp_feat_list_purge(&dccp_sk(sk)->dccps_featneg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	case DCCP_CLOSED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 		if (oldstate == DCCP_OPEN || oldstate == DCCP_ACTIVE_CLOSEREQ ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 		    oldstate == DCCP_CLOSING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 			DCCP_INC_STATS(DCCP_MIB_ESTABRESETS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 		sk->sk_prot->unhash(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 		if (inet_csk(sk)->icsk_bind_hash != NULL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 		    !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 			inet_put_port(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 		if (oldstate == DCCP_OPEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 			DCCP_DEC_STATS(DCCP_MIB_CURRESTAB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	/* Change state AFTER socket is unhashed to avoid closed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	 * socket sitting in hash tables.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	inet_sk_set_state(sk, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) EXPORT_SYMBOL_GPL(dccp_set_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) static void dccp_finish_passive_close(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	switch (sk->sk_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	case DCCP_PASSIVE_CLOSE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 		/* Node (client or server) has received Close packet. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 		dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 		dccp_set_state(sk, DCCP_CLOSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	case DCCP_PASSIVE_CLOSEREQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 		 * Client received CloseReq. We set the `active' flag so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 		 * dccp_send_close() retransmits the Close as per RFC 4340, 8.3.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 		dccp_send_close(sk, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 		dccp_set_state(sk, DCCP_CLOSING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) void dccp_done(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	dccp_set_state(sk, DCCP_CLOSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	dccp_clear_xmit_timers(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	sk->sk_shutdown = SHUTDOWN_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	if (!sock_flag(sk, SOCK_DEAD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 		sk->sk_state_change(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 		inet_csk_destroy_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) EXPORT_SYMBOL_GPL(dccp_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) const char *dccp_packet_name(const int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	static const char *const dccp_packet_names[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 		[DCCP_PKT_REQUEST]  = "REQUEST",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 		[DCCP_PKT_RESPONSE] = "RESPONSE",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 		[DCCP_PKT_DATA]	    = "DATA",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 		[DCCP_PKT_ACK]	    = "ACK",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 		[DCCP_PKT_DATAACK]  = "DATAACK",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 		[DCCP_PKT_CLOSEREQ] = "CLOSEREQ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 		[DCCP_PKT_CLOSE]    = "CLOSE",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 		[DCCP_PKT_RESET]    = "RESET",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 		[DCCP_PKT_SYNC]	    = "SYNC",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 		[DCCP_PKT_SYNCACK]  = "SYNCACK",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	if (type >= DCCP_NR_PKT_TYPES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 		return "INVALID";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 		return dccp_packet_names[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) EXPORT_SYMBOL_GPL(dccp_packet_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) static void dccp_sk_destruct(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	struct dccp_sock *dp = dccp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	dp->dccps_hc_tx_ccid = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	inet_sock_destruct(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	struct dccp_sock *dp = dccp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	struct inet_connection_sock *icsk = inet_csk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	icsk->icsk_rto		= DCCP_TIMEOUT_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	icsk->icsk_syn_retries	= sysctl_dccp_request_retries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	sk->sk_state		= DCCP_CLOSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	sk->sk_write_space	= dccp_write_space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	sk->sk_destruct		= dccp_sk_destruct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	icsk->icsk_sync_mss	= dccp_sync_mss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	dp->dccps_mss_cache	= 536;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	dp->dccps_rate_last	= jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	dp->dccps_role		= DCCP_ROLE_UNDEFINED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	dp->dccps_service	= DCCP_SERVICE_CODE_IS_ABSENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	dp->dccps_tx_qlen	= sysctl_dccp_tx_qlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	dccp_init_xmit_timers(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	INIT_LIST_HEAD(&dp->dccps_featneg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	/* control socket doesn't need feat nego */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	if (likely(ctl_sock_initialized))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 		return dccp_feat_init(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) EXPORT_SYMBOL_GPL(dccp_init_sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) void dccp_destroy_sock(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	struct dccp_sock *dp = dccp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	__skb_queue_purge(&sk->sk_write_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	if (sk->sk_send_head != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 		kfree_skb(sk->sk_send_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 		sk->sk_send_head = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	/* Clean up a referenced DCCP bind bucket. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	if (inet_csk(sk)->icsk_bind_hash != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 		inet_put_port(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	kfree(dp->dccps_service_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	dp->dccps_service_list = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	if (dp->dccps_hc_rx_ackvec != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 		dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 		dp->dccps_hc_rx_ackvec = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	dp->dccps_hc_rx_ccid = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	/* clean up feature negotiation state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	dccp_feat_list_purge(&dp->dccps_featneg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) EXPORT_SYMBOL_GPL(dccp_destroy_sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) static inline int dccp_listen_start(struct sock *sk, int backlog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	struct dccp_sock *dp = dccp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	dp->dccps_role = DCCP_ROLE_LISTEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	/* do not start to listen if feature negotiation setup fails */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	if (dccp_feat_finalise_settings(dp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 		return -EPROTO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	return inet_csk_listen_start(sk, backlog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) static inline int dccp_need_reset(int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	return state != DCCP_CLOSED && state != DCCP_LISTEN &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	       state != DCCP_REQUESTING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) int dccp_disconnect(struct sock *sk, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	struct inet_connection_sock *icsk = inet_csk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	struct inet_sock *inet = inet_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	struct dccp_sock *dp = dccp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	const int old_state = sk->sk_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	if (old_state != DCCP_CLOSED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 		dccp_set_state(sk, DCCP_CLOSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	 * This corresponds to the ABORT function of RFC793, sec. 3.8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	 * TCP uses a RST segment, DCCP a Reset packet with Code 2, "Aborted".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	if (old_state == DCCP_LISTEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 		inet_csk_listen_stop(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	} else if (dccp_need_reset(old_state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 		sk->sk_err = ECONNRESET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	} else if (old_state == DCCP_REQUESTING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		sk->sk_err = ECONNRESET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	dccp_clear_xmit_timers(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	dp->dccps_hc_rx_ccid = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	__skb_queue_purge(&sk->sk_receive_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	__skb_queue_purge(&sk->sk_write_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	if (sk->sk_send_head != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 		__kfree_skb(sk->sk_send_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 		sk->sk_send_head = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	inet->inet_dport = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 		inet_reset_saddr(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	sk->sk_shutdown = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	sock_reset_flag(sk, SOCK_DONE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	icsk->icsk_backoff = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	inet_csk_delack_init(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	__sk_dst_reset(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	WARN_ON(inet->inet_num && !icsk->icsk_bind_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	sk->sk_error_report(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) EXPORT_SYMBOL_GPL(dccp_disconnect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312)  *	Wait for a DCCP event.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314)  *	Note that we don't need to lock the socket, as the upper poll layers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315)  *	take care of normal races (between the test and the event) and we don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316)  *	go look at any of the socket buffers directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) __poll_t dccp_poll(struct file *file, struct socket *sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 		       poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	__poll_t mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	sock_poll_wait(file, sock, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	if (sk->sk_state == DCCP_LISTEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 		return inet_csk_listen_poll(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	/* Socket is not locked. We are protected from async events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	   by poll logic and correct handling of state changes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	   made by another threads is impossible in any case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	if (sk->sk_err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 		mask = EPOLLERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 		mask |= EPOLLHUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	if (sk->sk_shutdown & RCV_SHUTDOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 		mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	/* Connected? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		if (atomic_read(&sk->sk_rmem_alloc) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 			mask |= EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 		if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 			if (sk_stream_is_writeable(sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 				mask |= EPOLLOUT | EPOLLWRNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 			} else {  /* send SIGIO later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 				sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 				set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 				/* Race breaker. If space is freed after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 				 * wspace test but before the flags are set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 				 * IO signal will be lost.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 				if (sk_stream_is_writeable(sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 					mask |= EPOLLOUT | EPOLLWRNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) EXPORT_SYMBOL_GPL(dccp_poll);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	int rc = -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	if (sk->sk_state == DCCP_LISTEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	case SIOCOUTQ: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 		int amount = sk_wmem_alloc_get(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 		/* Using sk_wmem_alloc here because sk_wmem_queued is not used by DCCP and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 		 * always 0, comparably to UDP.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		rc = put_user(amount, (int __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	case SIOCINQ: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 		struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 		unsigned long amount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 		skb = skb_peek(&sk->sk_receive_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		if (skb != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 			 * We will only return the amount of this packet since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 			 * that is all that will be read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 			amount = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		rc = put_user(amount, (int __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 		rc = -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) EXPORT_SYMBOL_GPL(dccp_ioctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) static int dccp_setsockopt_service(struct sock *sk, const __be32 service,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 				   sockptr_t optval, unsigned int optlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	struct dccp_sock *dp = dccp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	struct dccp_service_list *sl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	if (service == DCCP_SERVICE_INVALID_VALUE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	    optlen > DCCP_SERVICE_LIST_MAX_LEN * sizeof(u32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	if (optlen > sizeof(service)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		sl = kmalloc(optlen, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		if (sl == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 		sl->dccpsl_nr = optlen / sizeof(u32) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 		if (copy_from_sockptr_offset(sl->dccpsl_list, optval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 				sizeof(service), optlen - sizeof(service)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 		    dccp_list_has_service(sl, DCCP_SERVICE_INVALID_VALUE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 			kfree(sl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	dp->dccps_service = service;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	kfree(dp->dccps_service_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	dp->dccps_service_list = sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) static int dccp_setsockopt_cscov(struct sock *sk, int cscov, bool rx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	u8 *list, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	int i, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	if (cscov < 0 || cscov > 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	 * Populate a list of permissible values, in the range cscov...15. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	 * is necessary since feature negotiation of single values only works if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	 * both sides incidentally choose the same value. Since the list starts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	 * lowest-value first, negotiation will pick the smallest shared value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	if (cscov == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	len = 16 - cscov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	list = kmalloc(len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	if (list == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 		return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	for (i = 0; i < len; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 		list[i] = cscov++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	rc = dccp_feat_register_sp(sk, DCCPF_MIN_CSUM_COVER, rx, list, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	if (rc == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 		if (rx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 			dccp_sk(sk)->dccps_pcrlen = cscov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 			dccp_sk(sk)->dccps_pcslen = cscov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	kfree(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) static int dccp_setsockopt_ccid(struct sock *sk, int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 				sockptr_t optval, unsigned int optlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	u8 *val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	if (optlen < 1 || optlen > DCCP_FEAT_MAX_SP_VALS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	val = memdup_sockptr(optval, optlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	if (IS_ERR(val))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 		return PTR_ERR(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	if (type == DCCP_SOCKOPT_TX_CCID || type == DCCP_SOCKOPT_CCID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		rc = dccp_feat_register_sp(sk, DCCPF_CCID, 1, val, optlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	if (!rc && (type == DCCP_SOCKOPT_RX_CCID || type == DCCP_SOCKOPT_CCID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		rc = dccp_feat_register_sp(sk, DCCPF_CCID, 0, val, optlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	kfree(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) static int do_dccp_setsockopt(struct sock *sk, int level, int optname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 		sockptr_t optval, unsigned int optlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	struct dccp_sock *dp = dccp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	int val, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	switch (optname) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	case DCCP_SOCKOPT_PACKET_SIZE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 		DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	case DCCP_SOCKOPT_CHANGE_L:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	case DCCP_SOCKOPT_CHANGE_R:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 		DCCP_WARN("sockopt(CHANGE_L/R) is deprecated: fix your app\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	case DCCP_SOCKOPT_CCID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	case DCCP_SOCKOPT_RX_CCID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	case DCCP_SOCKOPT_TX_CCID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		return dccp_setsockopt_ccid(sk, optname, optval, optlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	if (optlen < (int)sizeof(int))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	if (copy_from_sockptr(&val, optval, sizeof(int)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	if (optname == DCCP_SOCKOPT_SERVICE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 		return dccp_setsockopt_service(sk, val, optval, optlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	switch (optname) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	case DCCP_SOCKOPT_SERVER_TIMEWAIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		if (dp->dccps_role != DCCP_ROLE_SERVER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 			err = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 			dp->dccps_server_timewait = (val != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	case DCCP_SOCKOPT_SEND_CSCOV:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		err = dccp_setsockopt_cscov(sk, val, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	case DCCP_SOCKOPT_RECV_CSCOV:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		err = dccp_setsockopt_cscov(sk, val, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	case DCCP_SOCKOPT_QPOLICY_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 		if (sk->sk_state != DCCP_CLOSED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 			err = -EISCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 		else if (val < 0 || val >= DCCPQ_POLICY_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 			dp->dccps_qpolicy = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	case DCCP_SOCKOPT_QPOLICY_TXQLEN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 		if (val < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 			dp->dccps_tx_qlen = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 		err = -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) int dccp_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		    unsigned int optlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	if (level != SOL_DCCP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 		return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 							     optname, optval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 							     optlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	return do_dccp_setsockopt(sk, level, optname, optval, optlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) EXPORT_SYMBOL_GPL(dccp_setsockopt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) static int dccp_getsockopt_service(struct sock *sk, int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 				   __be32 __user *optval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 				   int __user *optlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	const struct dccp_sock *dp = dccp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	const struct dccp_service_list *sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	int err = -ENOENT, slen = 0, total_len = sizeof(u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	if ((sl = dp->dccps_service_list) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 		slen = sl->dccpsl_nr * sizeof(u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 		total_len += slen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	if (total_len > len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	if (put_user(total_len, optlen) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	    put_user(dp->dccps_service, optval) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	    (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 		err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) static int do_dccp_getsockopt(struct sock *sk, int level, int optname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		    char __user *optval, int __user *optlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	struct dccp_sock *dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	int val, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	if (get_user(len, optlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	if (len < (int)sizeof(int))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	dp = dccp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	switch (optname) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	case DCCP_SOCKOPT_PACKET_SIZE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 		DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	case DCCP_SOCKOPT_SERVICE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 		return dccp_getsockopt_service(sk, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 					       (__be32 __user *)optval, optlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	case DCCP_SOCKOPT_GET_CUR_MPS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 		val = dp->dccps_mss_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	case DCCP_SOCKOPT_AVAILABLE_CCIDS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		return ccid_getsockopt_builtin_ccids(sk, len, optval, optlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	case DCCP_SOCKOPT_TX_CCID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 		val = ccid_get_current_tx_ccid(dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		if (val < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 			return -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	case DCCP_SOCKOPT_RX_CCID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 		val = ccid_get_current_rx_ccid(dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 		if (val < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 			return -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	case DCCP_SOCKOPT_SERVER_TIMEWAIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 		val = dp->dccps_server_timewait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	case DCCP_SOCKOPT_SEND_CSCOV:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 		val = dp->dccps_pcslen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	case DCCP_SOCKOPT_RECV_CSCOV:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		val = dp->dccps_pcrlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	case DCCP_SOCKOPT_QPOLICY_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		val = dp->dccps_qpolicy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	case DCCP_SOCKOPT_QPOLICY_TXQLEN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		val = dp->dccps_tx_qlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	case 128 ... 191:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 		return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 					     len, (u32 __user *)optval, optlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	case 192 ... 255:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 		return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 					     len, (u32 __user *)optval, optlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 		return -ENOPROTOOPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	len = sizeof(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	if (put_user(len, optlen) || copy_to_user(optval, &val, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) int dccp_getsockopt(struct sock *sk, int level, int optname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 		    char __user *optval, int __user *optlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	if (level != SOL_DCCP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 		return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 							     optname, optval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 							     optlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	return do_dccp_getsockopt(sk, level, optname, optval, optlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) EXPORT_SYMBOL_GPL(dccp_getsockopt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) static int dccp_msghdr_parse(struct msghdr *msg, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	struct cmsghdr *cmsg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	 * Assign an (opaque) qpolicy priority value to skb->priority.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	 * We are overloading this skb field for use with the qpolicy subystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	 * The skb->priority is normally used for the SO_PRIORITY option, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	 * is initialised from sk_priority. Since the assignment of sk_priority
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	 * to skb->priority happens later (on layer 3), we overload this field
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	 * for use with queueing priorities as long as the skb is on layer 4.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	 * The default priority value (if nothing is set) is 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	skb->priority = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	for_each_cmsghdr(cmsg, msg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 		if (!CMSG_OK(msg, cmsg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 		if (cmsg->cmsg_level != SOL_DCCP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 		if (cmsg->cmsg_type <= DCCP_SCM_QPOLICY_MAX &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 		    !dccp_qpolicy_param_ok(skb->sk, cmsg->cmsg_type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 		switch (cmsg->cmsg_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		case DCCP_SCM_PRIORITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(__u32)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 			skb->priority = *(__u32 *)CMSG_DATA(cmsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 			return -EINVAL;
^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) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) int dccp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	const struct dccp_sock *dp = dccp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	const int flags = msg->msg_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	const int noblock = flags & MSG_DONTWAIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	int rc, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	long timeo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	trace_dccp_probe(sk, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	if (len > dp->dccps_mss_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 		return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	if (dccp_qpolicy_full(sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		rc = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 		goto out_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	timeo = sock_sndtimeo(sk, noblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	 * We have to use sk_stream_wait_connect here to set sk_write_pending,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	 * so that the trick in dccp_rcv_request_sent_state_process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	/* Wait for a connection to finish. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 		if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 			goto out_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	size = sk->sk_prot->max_header + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	skb = sock_alloc_send_skb(sk, size, noblock, &rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	if (skb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 		goto out_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	if (sk->sk_state == DCCP_CLOSED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		rc = -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 		goto out_discard;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	skb_reserve(skb, sk->sk_prot->max_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	rc = memcpy_from_msg(skb_put(skb, len), msg, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 		goto out_discard;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	rc = dccp_msghdr_parse(msg, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 		goto out_discard;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	dccp_qpolicy_push(sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	 * The xmit_timer is set if the TX CCID is rate-based and will expire
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	 * when congestion control permits to release further packets into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	 * network. Window-based CCIDs do not use this timer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	if (!timer_pending(&dp->dccps_xmit_timer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 		dccp_write_xmit(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) out_release:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	return rc ? : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) out_discard:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	goto out_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) EXPORT_SYMBOL_GPL(dccp_sendmsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) int dccp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		 int flags, int *addr_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	const struct dccp_hdr *dh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	long timeo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	if (sk->sk_state == DCCP_LISTEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 		len = -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	timeo = sock_rcvtimeo(sk, nonblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		if (skb == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 			goto verify_sock_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 		dh = dccp_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 		switch (dh->dccph_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 		case DCCP_PKT_DATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 		case DCCP_PKT_DATAACK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 			goto found_ok_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		case DCCP_PKT_CLOSE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 		case DCCP_PKT_CLOSEREQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 			if (!(flags & MSG_PEEK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 				dccp_finish_passive_close(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 			fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		case DCCP_PKT_RESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 			dccp_pr_debug("found fin (%s) ok!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 				      dccp_packet_name(dh->dccph_type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 			len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 			goto found_fin_ok;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 			dccp_pr_debug("packet_type=%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 				      dccp_packet_name(dh->dccph_type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 			sk_eat_skb(sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) verify_sock_status:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 		if (sock_flag(sk, SOCK_DONE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 			len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 		if (sk->sk_err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 			len = sock_error(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		if (sk->sk_shutdown & RCV_SHUTDOWN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 			len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 		if (sk->sk_state == DCCP_CLOSED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 			if (!sock_flag(sk, SOCK_DONE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 				/* This occurs when user tries to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 				 * from never connected socket.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 				len = -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 			len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 		if (!timeo) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 			len = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 		if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 			len = sock_intr_errno(timeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 		sk_wait_data(sk, &timeo, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 		continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	found_ok_skb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 		if (len > skb->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 			len = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 		else if (len < skb->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 			msg->msg_flags |= MSG_TRUNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		if (skb_copy_datagram_msg(skb, 0, msg, len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 			/* Exception. Bailout! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 			len = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 		if (flags & MSG_TRUNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 			len = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	found_fin_ok:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 		if (!(flags & MSG_PEEK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 			sk_eat_skb(sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	} while (1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) EXPORT_SYMBOL_GPL(dccp_recvmsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) int inet_dccp_listen(struct socket *sock, int backlog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	unsigned char old_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	old_state = sk->sk_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	WRITE_ONCE(sk->sk_max_ack_backlog, backlog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	/* Really, if the socket is already in listen state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	 * we can only allow the backlog to be adjusted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	if (old_state != DCCP_LISTEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 		 * FIXME: here it probably should be sk->sk_prot->listen_start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		 * see tcp_listen_start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 		err = dccp_listen_start(sk, backlog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) EXPORT_SYMBOL_GPL(inet_dccp_listen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) static void dccp_terminate_connection(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	u8 next_state = DCCP_CLOSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	switch (sk->sk_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	case DCCP_PASSIVE_CLOSE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	case DCCP_PASSIVE_CLOSEREQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 		dccp_finish_passive_close(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	case DCCP_PARTOPEN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		dccp_pr_debug("Stop PARTOPEN timer (%p)\n", sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	case DCCP_OPEN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		dccp_send_close(sk, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		if (dccp_sk(sk)->dccps_role == DCCP_ROLE_SERVER &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		    !dccp_sk(sk)->dccps_server_timewait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 			next_state = DCCP_ACTIVE_CLOSEREQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 			next_state = DCCP_CLOSING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		dccp_set_state(sk, next_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) void dccp_close(struct sock *sk, long timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	struct dccp_sock *dp = dccp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	u32 data_was_unread = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	int state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	sk->sk_shutdown = SHUTDOWN_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	if (sk->sk_state == DCCP_LISTEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		dccp_set_state(sk, DCCP_CLOSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 		/* Special case. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		inet_csk_listen_stop(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		goto adjudge_to_death;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	sk_stop_timer(sk, &dp->dccps_xmit_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	 * We need to flush the recv. buffs.  We do this only on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	 * descriptor close, not protocol-sourced closes, because the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	  *reader process may not have drained the data yet!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 		data_was_unread += skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 		__kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	/* If socket has been already reset kill it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	if (sk->sk_state == DCCP_CLOSED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		goto adjudge_to_death;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	if (data_was_unread) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		/* Unread data was tossed, send an appropriate Reset Code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		DCCP_WARN("ABORT with %u bytes unread\n", data_was_unread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 		dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 		dccp_set_state(sk, DCCP_CLOSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	} else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 		/* Check zero linger _after_ checking for unread data. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 		sk->sk_prot->disconnect(sk, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	} else if (sk->sk_state != DCCP_CLOSED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		 * Normal connection termination. May need to wait if there are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 		 * still packets in the TX queue that are delayed by the CCID.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 		dccp_flush_write_queue(sk, &timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 		dccp_terminate_connection(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	 * Flush write queue. This may be necessary in several cases:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	 * - we have been closed by the peer but still have application data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	 * - abortive termination (unread data or zero linger time),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	 * - normal termination but queue could not be flushed within time limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	__skb_queue_purge(&sk->sk_write_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	sk_stream_wait_close(sk, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) adjudge_to_death:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	state = sk->sk_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	sock_hold(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	sock_orphan(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	 * It is the last release_sock in its life. It will remove backlog.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	 * Now socket is owned by kernel and we acquire BH lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	 * to finish close. No need to check for user refs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	local_bh_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	bh_lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	WARN_ON(sock_owned_by_user(sk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	percpu_counter_inc(sk->sk_prot->orphan_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	/* Have we already been destroyed by a softirq or backlog? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	if (state != DCCP_CLOSED && sk->sk_state == DCCP_CLOSED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	if (sk->sk_state == DCCP_CLOSED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 		inet_csk_destroy_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	/* Otherwise, socket is reprieved until protocol close. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	bh_unlock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	local_bh_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) EXPORT_SYMBOL_GPL(dccp_close);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) void dccp_shutdown(struct sock *sk, int how)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	dccp_pr_debug("called shutdown(%x)\n", how);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) EXPORT_SYMBOL_GPL(dccp_shutdown);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) static inline int __init dccp_mib_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	dccp_statistics = alloc_percpu(struct dccp_mib);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	if (!dccp_statistics)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) static inline void dccp_mib_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	free_percpu(dccp_statistics);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) static int thash_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) module_param(thash_entries, int, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) MODULE_PARM_DESC(thash_entries, "Number of ehash buckets");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) #ifdef CONFIG_IP_DCCP_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) bool dccp_debug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) module_param(dccp_debug, bool, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) MODULE_PARM_DESC(dccp_debug, "Enable debug messages");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) EXPORT_SYMBOL_GPL(dccp_debug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) static int __init dccp_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	unsigned long goal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	unsigned long nr_pages = totalram_pages();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	int ehash_order, bhash_order, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	BUILD_BUG_ON(sizeof(struct dccp_skb_cb) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 		     sizeof_field(struct sk_buff, cb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	rc = percpu_counter_init(&dccp_orphan_count, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	inet_hashinfo_init(&dccp_hashinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	rc = inet_hashinfo2_init_mod(&dccp_hashinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 		goto out_free_percpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	rc = -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	dccp_hashinfo.bind_bucket_cachep =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 		kmem_cache_create("dccp_bind_bucket",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 				  sizeof(struct inet_bind_bucket), 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 				  SLAB_HWCACHE_ALIGN, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	if (!dccp_hashinfo.bind_bucket_cachep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 		goto out_free_hashinfo2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	 * Size and allocate the main established and bind bucket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	 * hash tables.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	 * The methodology is similar to that of the buffer cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	if (nr_pages >= (128 * 1024))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 		goal = nr_pages >> (21 - PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 		goal = nr_pages >> (23 - PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	if (thash_entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 		goal = (thash_entries *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 			sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 		;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 		unsigned long hash_size = (1UL << ehash_order) * PAGE_SIZE /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 					sizeof(struct inet_ehash_bucket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 		while (hash_size & (hash_size - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 			hash_size--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 		dccp_hashinfo.ehash_mask = hash_size - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 		dccp_hashinfo.ehash = (struct inet_ehash_bucket *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 			__get_free_pages(GFP_ATOMIC|__GFP_NOWARN, ehash_order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	} while (!dccp_hashinfo.ehash && --ehash_order > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	if (!dccp_hashinfo.ehash) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 		DCCP_CRIT("Failed to allocate DCCP established hash table");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 		goto out_free_bind_bucket_cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	for (i = 0; i <= dccp_hashinfo.ehash_mask; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 		INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].chain, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	if (inet_ehash_locks_alloc(&dccp_hashinfo))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 			goto out_free_dccp_ehash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	bhash_order = ehash_order;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 		dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 					sizeof(struct inet_bind_hashbucket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 		if ((dccp_hashinfo.bhash_size > (64 * 1024)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 		    bhash_order > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 		dccp_hashinfo.bhash = (struct inet_bind_hashbucket *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 			__get_free_pages(GFP_ATOMIC|__GFP_NOWARN, bhash_order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	} while (!dccp_hashinfo.bhash && --bhash_order >= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	if (!dccp_hashinfo.bhash) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 		DCCP_CRIT("Failed to allocate DCCP bind hash table");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 		goto out_free_dccp_locks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	for (i = 0; i < dccp_hashinfo.bhash_size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 		spin_lock_init(&dccp_hashinfo.bhash[i].lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 		INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	rc = dccp_mib_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		goto out_free_dccp_bhash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	rc = dccp_ackvec_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 		goto out_free_dccp_mib;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	rc = dccp_sysctl_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 		goto out_ackvec_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	rc = ccid_initialize_builtins();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 		goto out_sysctl_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	dccp_timestamping_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) out_sysctl_exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 	dccp_sysctl_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) out_ackvec_exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	dccp_ackvec_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) out_free_dccp_mib:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	dccp_mib_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) out_free_dccp_bhash:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) out_free_dccp_locks:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	inet_ehash_locks_free(&dccp_hashinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) out_free_dccp_ehash:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) out_free_bind_bucket_cachep:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) out_free_hashinfo2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	inet_hashinfo2_free_mod(&dccp_hashinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) out_free_percpu:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	percpu_counter_destroy(&dccp_orphan_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) out_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	dccp_hashinfo.bhash = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	dccp_hashinfo.ehash = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	dccp_hashinfo.bind_bucket_cachep = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) static void __exit dccp_fini(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	ccid_cleanup_builtins();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	dccp_mib_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	free_pages((unsigned long)dccp_hashinfo.bhash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 		   get_order(dccp_hashinfo.bhash_size *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 			     sizeof(struct inet_bind_hashbucket)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	free_pages((unsigned long)dccp_hashinfo.ehash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 		   get_order((dccp_hashinfo.ehash_mask + 1) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 			     sizeof(struct inet_ehash_bucket)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	inet_ehash_locks_free(&dccp_hashinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	dccp_ackvec_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	dccp_sysctl_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	inet_hashinfo2_free_mod(&dccp_hashinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	percpu_counter_destroy(&dccp_orphan_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) module_init(dccp_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) module_exit(dccp_fini);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol");