1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * NET		Generic infrastructure for INET connection oriented protocols.
4  *
5  *		Definitions for inet_connection_sock
6  *
7  * Authors:	Many people, see the TCP sources
8  *
9  * 		From code originally in TCP
10  */
11 #ifndef _INET_CONNECTION_SOCK_H
12 #define _INET_CONNECTION_SOCK_H
13 
14 #include <linux/compiler.h>
15 #include <linux/string.h>
16 #include <linux/timer.h>
17 #include <linux/poll.h>
18 #include <linux/kernel.h>
19 #include <linux/sockptr.h>
20 
21 #include <net/inet_sock.h>
22 #include <net/request_sock.h>
23 #if defined(CONFIG_TCP_NATA_URC) || defined(CONFIG_TCP_NATA_STL)
24 #include <net/nata.h>
25 #include <net/inet_connection_nata.h>
26 #endif
27 
28 /* Cancel timers, when they are not required. */
29 #undef INET_CSK_CLEAR_TIMERS
30 
31 struct inet_bind_bucket;
32 struct tcp_congestion_ops;
33 
34 /*
35  * Pointers to address related TCP functions
36  * (i.e. things that depend on the address family)
37  */
38 struct inet_connection_sock_af_ops {
39 	int	    (*queue_xmit)(struct sock *sk, struct sk_buff *skb, struct flowi *fl);
40 	void	    (*send_check)(struct sock *sk, struct sk_buff *skb);
41 	int	    (*rebuild_header)(struct sock *sk);
42 	void	    (*sk_rx_dst_set)(struct sock *sk, const struct sk_buff *skb);
43 	int	    (*conn_request)(struct sock *sk, struct sk_buff *skb);
44 	struct sock *(*syn_recv_sock)(const struct sock *sk, struct sk_buff *skb,
45 				      struct request_sock *req,
46 				      struct dst_entry *dst,
47 				      struct request_sock *req_unhash,
48 				      bool *own_req);
49 	u16	    net_header_len;
50 	u16	    net_frag_header_len;
51 	u16	    sockaddr_len;
52 	int	    (*setsockopt)(struct sock *sk, int level, int optname,
53 				  sockptr_t optval, unsigned int optlen);
54 	int	    (*getsockopt)(struct sock *sk, int level, int optname,
55 				  char __user *optval, int __user *optlen);
56 	void	    (*addr2sockaddr)(struct sock *sk, struct sockaddr *);
57 	void	    (*mtu_reduced)(struct sock *sk);
58 };
59 
60 /** inet_connection_sock - INET connection oriented sock
61  *
62  * @icsk_accept_queue:	   FIFO of established children
63  * @icsk_bind_hash:	   Bind node
64  * @icsk_timeout:	   Timeout
65  * @icsk_retransmit_timer: Resend (no ack)
66  * @icsk_rto:		   Retransmit timeout
67  * @icsk_pmtu_cookie	   Last pmtu seen by socket
68  * @icsk_ca_ops		   Pluggable congestion control hook
69  * @icsk_af_ops		   Operations which are AF_INET{4,6} specific
70  * @icsk_ulp_ops	   Pluggable ULP control hook
71  * @icsk_ulp_data	   ULP private data
72  * @icsk_clean_acked	   Clean acked data hook
73  * @icsk_listen_portaddr_node	hash to the portaddr listener hashtable
74  * @icsk_ca_state:	   Congestion control state
75  * @icsk_retransmits:	   Number of unrecovered [RTO] timeouts
76  * @icsk_pending:	   Scheduled timer event
77  * @icsk_backoff:	   Backoff
78  * @icsk_syn_retries:      Number of allowed SYN (or equivalent) retries
79  * @icsk_probes_out:	   unanswered 0 window probes
80  * @icsk_ext_hdr_len:	   Network protocol overhead (IP/IPv6 options)
81  * @icsk_ack:		   Delayed ACK control data
82  * @icsk_mtup;		   MTU probing control data
83  * @icsk_probes_tstamp:    Probe timestamp (cleared by non-zero window ack)
84  * @icsk_user_timeout:	   TCP_USER_TIMEOUT value
85  */
86 struct inet_connection_sock {
87 	/* inet_sock has to be the first member! */
88 	struct inet_sock	  icsk_inet;
89 	struct request_sock_queue icsk_accept_queue;
90 	struct inet_bind_bucket	  *icsk_bind_hash;
91 	unsigned long		  icsk_timeout;
92  	struct timer_list	  icsk_retransmit_timer;
93  	struct timer_list	  icsk_delack_timer;
94 	__u32			  icsk_rto;
95 	__u32                     icsk_rto_min;
96 	__u32                     icsk_delack_max;
97 	__u32			  icsk_pmtu_cookie;
98 	const struct tcp_congestion_ops *icsk_ca_ops;
99 	const struct inet_connection_sock_af_ops *icsk_af_ops;
100 	const struct tcp_ulp_ops  *icsk_ulp_ops;
101 	void __rcu		  *icsk_ulp_data;
102 	void (*icsk_clean_acked)(struct sock *sk, u32 acked_seq);
103 	struct hlist_node         icsk_listen_portaddr_node;
104 	unsigned int		  (*icsk_sync_mss)(struct sock *sk, u32 pmtu);
105 	__u8			  icsk_ca_state:5,
106 				  icsk_ca_initialized:1,
107 				  icsk_ca_setsockopt:1,
108 				  icsk_ca_dst_locked:1;
109 	__u8			  icsk_retransmits;
110 	__u8			  icsk_pending;
111 	__u8			  icsk_backoff;
112 	__u8			  icsk_syn_retries;
113 	__u8			  icsk_probes_out;
114 	__u16			  icsk_ext_hdr_len;
115 #if defined(CONFIG_TCP_NATA_URC) || defined(CONFIG_TCP_NATA_STL)
116 	__u8			  nata_retries_enabled:1,
117 				  nata_reserved:7;
118 	__u8			  nata_data_retries;
119 	__u8			  nata_retries_type;
120 	__u32			  nata_syn_rto;
121 	__u32			  nata_data_rto;
122 #endif
123 	struct {
124 		__u8		  pending;	 /* ACK is pending			   */
125 		__u8		  quick;	 /* Scheduled number of quick acks	   */
126 		__u8		  pingpong;	 /* The session is interactive		   */
127 		__u8		  retry;	 /* Number of attempts			   */
128 		__u32		  ato;		 /* Predicted tick of soft clock	   */
129 		unsigned long	  timeout;	 /* Currently scheduled timeout		   */
130 		__u32		  lrcvtime;	 /* timestamp of last received data packet */
131 		__u16		  last_seg_size; /* Size of last incoming segment	   */
132 		__u16		  rcv_mss;	 /* MSS used for delayed ACK decisions	   */
133 	} icsk_ack;
134 	struct {
135 		int		  enabled;
136 
137 		/* Range of MTUs to search */
138 		int		  search_high;
139 		int		  search_low;
140 
141 		/* Information on the current probe. */
142 		int		  probe_size;
143 
144 		u32		  probe_timestamp;
145 	} icsk_mtup;
146 	u32			  icsk_probes_tstamp;
147 	u32			  icsk_user_timeout;
148 
149 	u64			  icsk_ca_priv[104 / sizeof(u64)];
150 #define ICSK_CA_PRIV_SIZE      (13 * sizeof(u64))
151 };
152 
153 #define ICSK_TIME_RETRANS	1	/* Retransmit timer */
154 #define ICSK_TIME_DACK		2	/* Delayed ack timer */
155 #define ICSK_TIME_PROBE0	3	/* Zero window probe timer */
156 #define ICSK_TIME_EARLY_RETRANS 4	/* Early retransmit timer */
157 #define ICSK_TIME_LOSS_PROBE	5	/* Tail loss probe timer */
158 #define ICSK_TIME_REO_TIMEOUT	6	/* Reordering timer */
159 
inet_csk(const struct sock *sk)160 static inline struct inet_connection_sock *inet_csk(const struct sock *sk)
161 {
162 	return (struct inet_connection_sock *)sk;
163 }
164 
inet_csk_ca(const struct sock *sk)165 static inline void *inet_csk_ca(const struct sock *sk)
166 {
167 	return (void *)inet_csk(sk)->icsk_ca_priv;
168 }
169 
170 struct sock *inet_csk_clone_lock(const struct sock *sk,
171 				 const struct request_sock *req,
172 				 const gfp_t priority);
173 
174 enum inet_csk_ack_state_t {
175 	ICSK_ACK_SCHED	= 1,
176 	ICSK_ACK_TIMER  = 2,
177 	ICSK_ACK_PUSHED = 4,
178 	ICSK_ACK_PUSHED2 = 8,
179 	ICSK_ACK_NOW = 16	/* Send the next ACK immediately (once) */
180 };
181 
182 void inet_csk_init_xmit_timers(struct sock *sk,
183 			       void (*retransmit_handler)(struct timer_list *),
184 			       void (*delack_handler)(struct timer_list *),
185 			       void (*keepalive_handler)(struct timer_list *));
186 void inet_csk_clear_xmit_timers(struct sock *sk);
187 void inet_csk_clear_xmit_timers_sync(struct sock *sk);
188 
inet_csk_schedule_ack(struct sock *sk)189 static inline void inet_csk_schedule_ack(struct sock *sk)
190 {
191 	inet_csk(sk)->icsk_ack.pending |= ICSK_ACK_SCHED;
192 }
193 
inet_csk_ack_scheduled(const struct sock *sk)194 static inline int inet_csk_ack_scheduled(const struct sock *sk)
195 {
196 	return inet_csk(sk)->icsk_ack.pending & ICSK_ACK_SCHED;
197 }
198 
inet_csk_delack_init(struct sock *sk)199 static inline void inet_csk_delack_init(struct sock *sk)
200 {
201 	memset(&inet_csk(sk)->icsk_ack, 0, sizeof(inet_csk(sk)->icsk_ack));
202 }
203 
204 void inet_csk_delete_keepalive_timer(struct sock *sk);
205 void inet_csk_reset_keepalive_timer(struct sock *sk, unsigned long timeout);
206 
inet_csk_clear_xmit_timer(struct sock *sk, const int what)207 static inline void inet_csk_clear_xmit_timer(struct sock *sk, const int what)
208 {
209 	struct inet_connection_sock *icsk = inet_csk(sk);
210 
211 	if (what == ICSK_TIME_RETRANS || what == ICSK_TIME_PROBE0) {
212 		icsk->icsk_pending = 0;
213 #ifdef INET_CSK_CLEAR_TIMERS
214 		sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
215 #endif
216 	} else if (what == ICSK_TIME_DACK) {
217 		icsk->icsk_ack.pending = 0;
218 		icsk->icsk_ack.retry = 0;
219 #ifdef INET_CSK_CLEAR_TIMERS
220 		sk_stop_timer(sk, &icsk->icsk_delack_timer);
221 #endif
222 	} else {
223 		pr_debug("inet_csk BUG: unknown timer value\n");
224 	}
225 }
226 
227 #if defined(CONFIG_TCP_NATA_URC) || defined(CONFIG_TCP_NATA_STL)
get_nata_rto(struct sock *sk, struct inet_connection_sock *icsk, unsigned long when, const int what)228 static inline unsigned long get_nata_rto(struct sock *sk,
229 					 struct inet_connection_sock *icsk,
230 					 unsigned long when, const int what)
231 {
232 	unsigned long when_nata;
233 	unsigned long shift;
234 
235 	if (!icsk->nata_retries_enabled)
236 		return when;
237 
238 	switch (what) {
239 	case ICSK_TIME_RETRANS:
240 	case ICSK_TIME_EARLY_RETRANS:
241 	case ICSK_TIME_LOSS_PROBE:
242 	case ICSK_TIME_REO_TIMEOUT:
243 		break;
244 	default:
245 		return when;
246 	}
247 
248 	if (icsk->nata_retries_type == NATA_STL)
249 		return sk->sk_state == TCP_SYN_SENT ?
250 			icsk->nata_syn_rto : icsk->nata_data_rto;
251 
252 	if (!nata_thin_stream_check(sk))
253 		return when;
254 
255 	when_nata = icsk->nata_data_rto;
256 	if (icsk->icsk_retransmits > icsk->nata_data_retries) {
257 		shift = icsk->icsk_retransmits - icsk->nata_data_retries;
258 		if (shift > MAX_SHIFT) {
259 			when_nata = NATA_RTO_MAX;
260 		} else {
261 			when_nata <<= shift;
262 		}
263 	}
264 	return min(when, when_nata);
265 }
266 #endif
267 
268 /*
269  *	Reset the retransmission timer
270  */
inet_csk_reset_xmit_timer(struct sock *sk, const int what, unsigned long when, const unsigned long max_when)271 static inline void inet_csk_reset_xmit_timer(struct sock *sk, const int what,
272 					     unsigned long when,
273 					     const unsigned long max_when)
274 {
275 	struct inet_connection_sock *icsk = inet_csk(sk);
276 
277 #if defined(CONFIG_TCP_NATA_URC) || defined(CONFIG_TCP_NATA_STL)
278 	when = get_nata_rto(sk, icsk, when, what);
279 #endif
280 
281 	if (when > max_when) {
282 		pr_debug("reset_xmit_timer: sk=%p %d when=0x%lx, caller=%p\n",
283 			 sk, what, when, (void *)_THIS_IP_);
284 		when = max_when;
285 	}
286 
287 	if (what == ICSK_TIME_RETRANS || what == ICSK_TIME_PROBE0 ||
288 	    what == ICSK_TIME_EARLY_RETRANS || what == ICSK_TIME_LOSS_PROBE ||
289 	    what == ICSK_TIME_REO_TIMEOUT) {
290 		icsk->icsk_pending = what;
291 		icsk->icsk_timeout = jiffies + when;
292 		sk_reset_timer(sk, &icsk->icsk_retransmit_timer, icsk->icsk_timeout);
293 	} else if (what == ICSK_TIME_DACK) {
294 		icsk->icsk_ack.pending |= ICSK_ACK_TIMER;
295 		icsk->icsk_ack.timeout = jiffies + when;
296 		sk_reset_timer(sk, &icsk->icsk_delack_timer, icsk->icsk_ack.timeout);
297 	} else {
298 		pr_debug("inet_csk BUG: unknown timer value\n");
299 	}
300 }
301 
302 static inline unsigned long
inet_csk_rto_backoff(const struct inet_connection_sock *icsk, unsigned long max_when)303 inet_csk_rto_backoff(const struct inet_connection_sock *icsk,
304 		     unsigned long max_when)
305 {
306         u64 when = (u64)icsk->icsk_rto << icsk->icsk_backoff;
307 
308         return (unsigned long)min_t(u64, when, max_when);
309 }
310 
311 struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern);
312 
313 int inet_csk_get_port(struct sock *sk, unsigned short snum);
314 
315 struct dst_entry *inet_csk_route_req(const struct sock *sk, struct flowi4 *fl4,
316 				     const struct request_sock *req);
317 struct dst_entry *inet_csk_route_child_sock(const struct sock *sk,
318 					    struct sock *newsk,
319 					    const struct request_sock *req);
320 
321 struct sock *inet_csk_reqsk_queue_add(struct sock *sk,
322 				      struct request_sock *req,
323 				      struct sock *child);
324 void inet_csk_reqsk_queue_hash_add(struct sock *sk, struct request_sock *req,
325 				   unsigned long timeout);
326 struct sock *inet_csk_complete_hashdance(struct sock *sk, struct sock *child,
327 					 struct request_sock *req,
328 					 bool own_req);
329 
inet_csk_reqsk_queue_added(struct sock *sk)330 static inline void inet_csk_reqsk_queue_added(struct sock *sk)
331 {
332 	reqsk_queue_added(&inet_csk(sk)->icsk_accept_queue);
333 }
334 
inet_csk_reqsk_queue_len(const struct sock *sk)335 static inline int inet_csk_reqsk_queue_len(const struct sock *sk)
336 {
337 	return reqsk_queue_len(&inet_csk(sk)->icsk_accept_queue);
338 }
339 
inet_csk_reqsk_queue_is_full(const struct sock *sk)340 static inline int inet_csk_reqsk_queue_is_full(const struct sock *sk)
341 {
342 	return inet_csk_reqsk_queue_len(sk) >= sk->sk_max_ack_backlog;
343 }
344 
345 bool inet_csk_reqsk_queue_drop(struct sock *sk, struct request_sock *req);
346 void inet_csk_reqsk_queue_drop_and_put(struct sock *sk, struct request_sock *req);
347 
inet_csk_prepare_for_destroy_sock(struct sock *sk)348 static inline void inet_csk_prepare_for_destroy_sock(struct sock *sk)
349 {
350 	/* The below has to be done to allow calling inet_csk_destroy_sock */
351 	sock_set_flag(sk, SOCK_DEAD);
352 	this_cpu_inc(*sk->sk_prot->orphan_count);
353 }
354 
355 void inet_csk_destroy_sock(struct sock *sk);
356 void inet_csk_prepare_forced_close(struct sock *sk);
357 
358 /*
359  * LISTEN is a special case for poll..
360  */
inet_csk_listen_poll(const struct sock *sk)361 static inline __poll_t inet_csk_listen_poll(const struct sock *sk)
362 {
363 	return !reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue) ?
364 			(EPOLLIN | EPOLLRDNORM) : 0;
365 }
366 
367 int inet_csk_listen_start(struct sock *sk, int backlog);
368 void inet_csk_listen_stop(struct sock *sk);
369 
370 void inet_csk_addr2sockaddr(struct sock *sk, struct sockaddr *uaddr);
371 
372 /* update the fast reuse flag when adding a socket */
373 void inet_csk_update_fastreuse(struct inet_bind_bucket *tb,
374 			       struct sock *sk);
375 
376 struct dst_entry *inet_csk_update_pmtu(struct sock *sk, u32 mtu);
377 
378 #define TCP_PINGPONG_THRESH	1
379 
inet_csk_enter_pingpong_mode(struct sock *sk)380 static inline void inet_csk_enter_pingpong_mode(struct sock *sk)
381 {
382 	inet_csk(sk)->icsk_ack.pingpong = TCP_PINGPONG_THRESH;
383 }
384 
inet_csk_exit_pingpong_mode(struct sock *sk)385 static inline void inet_csk_exit_pingpong_mode(struct sock *sk)
386 {
387 	inet_csk(sk)->icsk_ack.pingpong = 0;
388 }
389 
inet_csk_in_pingpong_mode(struct sock *sk)390 static inline bool inet_csk_in_pingpong_mode(struct sock *sk)
391 {
392 	return inet_csk(sk)->icsk_ack.pingpong >= TCP_PINGPONG_THRESH;
393 }
394 
inet_csk_has_ulp(struct sock *sk)395 static inline bool inet_csk_has_ulp(struct sock *sk)
396 {
397 	return inet_sk(sk)->is_icsk && !!inet_csk(sk)->icsk_ulp_ops;
398 }
399 
inet_init_csk_locks(struct sock *sk)400 static inline void inet_init_csk_locks(struct sock *sk)
401 {
402 	struct inet_connection_sock *icsk = inet_csk(sk);
403 
404 	spin_lock_init(&icsk->icsk_accept_queue.rskq_lock);
405 	spin_lock_init(&icsk->icsk_accept_queue.fastopenq.lock);
406 }
407 
408 #endif /* _INET_CONNECTION_SOCK_H */
409